﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace MessengerBot
{
    /// <summary>
    /// This <see cref="ILogger"/> implementation writes to <see cref="Console.Out"/>.
    /// </summary>
    public sealed class ConsoleLogger : LoggerBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
        /// </summary>
        public ConsoleLogger()
            : this(null)
        {
            // Do nothing here
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleLogger"/> class.
        /// </summary>
        /// <param name="filter">A predicate that takes a <see cref="LogEntry"/> value and has
        /// to return <c>true</c> for the log entry to be sent to the
        /// log destination.</param>
        public ConsoleLogger(Predicate<LogEntry> filter)
            : base(filter)
        {
            // Do nothing here
        }

        /// <summary>
        /// When implemented in a descendant, will output the log message to the log destination.
        /// </summary>
        /// <param name="level">The level of the message.</param>
        /// <param name="message">The message to log.</param>
        protected override void WriteToLog(LogLevel level, string message)
        {
            string formatted = StandardFormat(level, message);

            if (level == LogLevel.Error)
                Console.Error.WriteLine(formatted);

            Console.Out.WriteLine(formatted);
        }
    }
}